package com.hspedu.set_;

import java.util.HashSet;
import java.util.Objects;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/8
 * @time 18:37
 */
public class HashSetExercise02 {
    public static void main(String[] args) {
        Employee2 jack = new Employee2("jack", 100, new MyDate(2002, 5, 13));
        Employee2 jack2 = new Employee2("jack", 100, new MyDate(2002, 5, 13));

        HashSet hs = new HashSet();
        hs.add(jack);
        hs.add(jack2);

        System.out.println("hs = " + hs.toString());
    }
}

class MyDate{
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyDate myDate = (MyDate) o;
        return year == myDate.year &&
                month == myDate.month &&
                day == myDate.day;
    }

    @Override
    public int hashCode() {
        return Objects.hash(year, month, day);
    }
}

class Employee2{
    private String name;
    private double sal;
    private MyDate birthday;

    public Employee2(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }



    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee2 employee2 = (Employee2) o;
        return Objects.equals(name, employee2.name) &&
                Objects.equals(birthday, employee2.birthday);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, birthday);
    }
}
